package com.xigua.xcar.net.update;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.util.Log;

import com.bantouyan.json.Json;
import com.bantouyan.json.JsonObject;
import com.xigua.xcar.net.AbstractData;
import com.xigua.xcar.net.AbstractDataManager;

public class UpdateDataManager extends AbstractDataManager {

	private static final String tag = "UpdateDataManager";
	private int requestcode = 1125;
	private int verCode = 1000;
	private UpdateData mData = null;
	
	public UpdateDataManager(Context context) {
		super(context);
	}
	
	@Override
	public List<NameValuePair> getRequestParam() {
		List<NameValuePair> postParamList = new ArrayList<NameValuePair>();
		BasicNameValuePair nameValuePair1  = new BasicNameValuePair("requestcode",""+requestcode);        
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param1", verCode + "");
        postParamList.add(nameValuePair1);
		return postParamList;
	}

	@Override
	public AbstractData getData() {
		return mData;
	}

	@Override
	public AbstractData afterParseData(String ss) {
		Log.i(tag, "orign:ss:" + ss);
		String array[] = ss.split(";");
		if(null != array){
			if(null != array[0]){
				ss = array[0];
			}
		}
		ss = ss.replace("\n", "");
		if(ss.indexOf("{")!=-1){
			ss = ss.substring(ss.indexOf("{"));
		}
		if(ss.indexOf("}")!= -1){
			ss = ss.substring(0, ss.indexOf("}")+1);
		}
		Log.i(tag, "ss:" + ss);
		try{
		JsonObject jsonArray = (JsonObject)Json.parseJsonText(ss);
		if(null != jsonArray){
			String updateDesc = null;
			String isNeeedUpdate = null;
			String url = null;
			isNeeedUpdate = jsonArray.getString("bNeedUpGrade");
			updateDesc = jsonArray.getString("upGradeInfo");
			url = jsonArray.getString("upGradeUrl");
			mData = new UpdateData();
			if(null == updateDesc || updateDesc.equals("null") || updateDesc.equals("")){
				mData.setmUpdateDesc(null);
			}else{
				mData.setmUpdateDesc(updateDesc);
				Log.i(tag, "updateDesc:"+updateDesc);
			}
			int ii = Integer.parseInt(isNeeedUpdate);
			if(0 == ii){
				mData.setmIsNeedUpdate(false);
			}else{
				mData.setmIsNeedUpdate(true);
			}
			if(null == url || "null".equals(url)){
				mData.setmUrl(null);
			}else{
				mData.setmUrl(url);
			}
		}else{
			mData = null;
		}
		
		}catch(Exception e){
			e.printStackTrace();
			mData = null;
		}
		return mData;
	}

}
